<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>画师动态收藏夹</title>
    <style>
        :root {
        --primary-color: #4361ee;
        --hover-color: #3a56d4;
        }
        body {
        font-family: 'Segoe UI', system-ui, sans-serif;
        margin: 0;
        padding: 20px;
        background-color: #f8f9fa;
        }
        .container {
        max-width: 1200px;
        margin: 0 auto;
        }
        .add-form {
        background: white;
        padding: 20px;
        border-radius: 10px;
        box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        margin-bottom: 30px;
        }
        .form-grid {
        display: grid;
        grid-template-columns: 1fr 1fr 120px 100px;
        gap: 10px;
        align-items: end;
        }
        @media (max-width: 600px) {
        .form-grid {
            grid-template-columns: 1fr;
        }
        }
        .form-group label {
        display: block;
        margin-bottom: 5px;
        font-weight: 500;
        color: #555;
        }
        .form-group input,
        .form-group select {
        width: 100%;
        padding: 6px 10px;
        border: 1px solid #ddd;
        border-radius: 5px;    
        font-size: 14px;  
        box-sizing: border-box;
        transition: border 0.2s;
        }
        .form-group input:focus {
        outline: none;
        border: 1px solid var(--primary-color);
        box-shadow: 0 0 0 2px rgba(67, 97, 238, 0.15);
        }
        button {
        background-color: var(--primary-color);
        color: white;
        border: none;
        padding: 9px 16px;
        border-radius: 6px;
        cursor: pointer;
        font-weight: 500;
        transition: background 0.2s;
        }
        button:hover {
        background-color: var(--hover-color);
        }
        .gallery {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
        gap: 20px;
        }
        .creator-card {
        background: white;
        border-radius: 10px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        padding: 15px;
        text-align: center;
        transition: transform 0.2s, box-shadow 0.2s;
        position: relative;
        }
        .creator-card:hover {
        transform: translateY(-3px);
        box-shadow: 0 4px 12px rgba(0,0,0,0.15);
        }
        .avatar {
        width: 80px;
        height: 80px;
        border-radius: 50%;
        object-fit: cover;
        margin: 0 auto 12px;
        border: 2px solid #f0f0f0;
        background-color: #f5f5f5;
        }
        .profile-link {
        display: block;
        color: var(--primary-color);
        text-decoration: none;
        font-size: 14px;
        word-break: break-word;
        line-height: 1.4;
        }
        .profile-link:hover {
        text-decoration: underline;
        }
        .data-actions {
        margin-top: 20px;
        display: flex;
        gap: 10px;
        }
        .secondary-btn {
        background: #f0f0f0;
        color: #333;
        }
        .secondary-btn:hover {
        background: #e5e5e5;
        }
        .delete-btn {
        position: absolute;
        bottom: 10px;
        right: 10px;
        background: #ff4757;
        color: white;
        border: none;
        width: 24px;
        height: 24px;
        border-radius: 50%;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        line-height: 1;
        padding: 0;
        font-size: 15px;
        transition: all 0.2s;
        }
        .edit-btn {
        position: absolute;
        bottom: 10px;
        right: 40px;
        background: #ffa502;
        color: white;
        border: none;
        width: 24px;
        height: 24px;
        border-radius: 50%;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        line-height: 1;
        padding: 0;
        font-size: 13px;
    transition: all 0.2s;
    }
    .edit-form {
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        background: white;
        padding: 20px;
        border-radius: 10px;
        box-shadow: 0 0 20px rgba(0,0,0,0.2);
        z-index: 100;
        width: 300px;
        }
        .edit-form h3 {
        margin-top: 0;
        }
        .edit-form-buttons {
        display: flex;
        gap: 10px;
        margin-top: 15px;
        }
        .overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0,0,0,0.5);
        z-index: 99;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>画师收藏夹</h1>
        
        <div class="add-form">
        <h3>添加新画师</h3>
        <form id="addForm">
            <div class="form-grid">
                <div class="form-group">
                <label for="creatorName">画师昵称</label>
                <input type="text" id="creatorName" placeholder="例如: 画师小A" required>
                </div>
                <div class="form-group">
                <label for="creatorId">用户ID</label>
                <input type="number" id="creatorId" placeholder="从链接中获取的数字ID" required>
                </div>
                <div class="form-group">
                <label for="platformType">平台</label>
                <select id="platformType" required>
                <option value="patreon">pateon</option>
                <option value="fanbox">fanbox</option>
                <option value="fantia">fantia</option>
                <option value="gumroad">gumroad</option>
                </select>
                </div>
                <button type="submit">添加</button>
            </div>
        </form>
        </div>

        <div class="data-actions">
        <button id="exportBtn" class="secondary-btn">导出数据</button>
        <button id="importBtn" class="secondary-btn">导入数据</button>
        <input type="file" id="fileInput" accept=".json" style="display: none;">
        <button id="clearBtn" class="secondary-btn">清空所有</button>
        </div>

        <div class="gallery" id="gallery"></div>
    </div>

    <div id="editFormContainer" style="display: none;">
        <div class="overlay"></div>
        <div class="edit-form">
        <h3>编辑画师</h3>
        <form id="editForm">
            <input type="hidden" id="editIndex">
            <div class="form-group">
                <label for="editName">画师昵称</label>
                <input type="text" id="editName" required>
            </div>
            <div class="form-group">
                <label for="editId">用户ID</label>
                <input type="number" id="editId" required>
            </div>
            <div class="form-group">
                <label for="editPlatform">平台</label>
                <select id="editPlatform" required>
                <option value="patreon">pateon</option>
                <option value="fanbox">fanbox</option>
                <option value="fantia">fantia</option>
                <option value="gumroad">gumroad</option>
                </select>
            </div>
            <div class="edit-form-buttons">
                <button type="submit">保存</button>
                <button type="button" id="cancelEdit">取消</button>
            </div>
        </form>
        </div>
    </div>

    <script>
        let creators = JSON.parse(localStorage.getItem('kemonoCreators')) || [];
        const gallery = document.getElementById('gallery');
        const addForm = document.getElementById('addForm');
        const exportBtn = document.getElementById('exportBtn');
        const importBtn = document.getElementById('importBtn');
        const fileInput = document.getElementById('fileInput');
        const clearBtn = document.getElementById('clearBtn');
        const editFormContainer = document.getElementById('editFormContainer');
        const editForm = document.getElementById('editForm');
        const cancelEdit = document.getElementById('cancelEdit');

        function renderCreators() {
        gallery.innerHTML = creators.map((creator, index) => {
            return `
            <div class="creator-card">
                <img src="https://img.kemono.su/icons/${creator.platform}/${creator.id}" 
                 class="avatar"
                 alt="${creator.name}的头像">
                <a href="https://kemono.su/${creator.platform}/user/${creator.id}" 
                   class="profile-link" 
                   target="_blank">${creator.name}</a>
                <button class="edit-btn" data-index="${index}">✏️</button>
                <button class="delete-btn" data-index="${index}">×</button>
            </div>
            `;
        }).join('');

        document.querySelectorAll('.delete-btn').forEach(btn => {
            btn.addEventListener('click', function() {
                const index = parseInt(this.getAttribute('data-index'));
                if (confirm(`确定删除 ${creators[index].name} 吗？`)) {
                creators.splice(index, 1);
                localStorage.setItem('kemonoCreators', JSON.stringify(creators));
                renderCreators();
                }
            });
        });

        document.querySelectorAll('.edit-btn').forEach(btn => {
            btn.addEventListener('click', function() {
                const index = parseInt(this.getAttribute('data-index'));
                showEditForm(index);
            });
        });
        }

        function showEditForm(index) {
        const creator = creators[index];
        document.getElementById('editIndex').value = index;
        document.getElementById('editName').value = creator.name;
        document.getElementById('editId').value = creator.id;
        document.getElementById('editPlatform').value = creator.platform;
        editFormContainer.style.display = 'block';
        }

        function hideEditForm() {
        editFormContainer.style.display = 'none';
        }

        function updateCreator(index, name, id, platform) {
        creators[index] = { name, id, platform };
        localStorage.setItem('kemonoCreators', JSON.stringify(creators));
        renderCreators();
        hideEditForm();
        }

        addForm.addEventListener('submit', function(e) {
        e.preventDefault();
        
        const newCreator = {
            name: document.getElementById('creatorName').value.trim(),
            id: document.getElementById('creatorId').value,
            platform: document.getElementById('platformType').value
        };

        const exists = creators.some(c => 
            c.id === newCreator.id && c.platform === newCreator.platform
        );
        
        if (exists) {
            alert('该画师已存在！');
            return;
        }

        creators.push(newCreator);
        localStorage.setItem('kemonoCreators', JSON.stringify(creators));
        renderCreators();
        this.reset();
        });

        editForm.addEventListener('submit', function(e) {
        e.preventDefault();
        const index = parseInt(document.getElementById('editIndex').value);
        const name = document.getElementById('editName').value.trim();
        const id = document.getElementById('editId').value;
        const platform = document.getElementById('editPlatform').value;
        
        updateCreator(index, name, id, platform);
        });

        cancelEdit.addEventListener('click', hideEditForm);

        exportBtn.addEventListener('click', () => {
        const dataStr = JSON.stringify(creators, null, 2);
        const blob = new Blob([dataStr], { type: 'application/json' });
        const url = URL.createObjectURL(blob);
        
        const a = document.createElement('a');
        a.href = url;
        a.download = `kemono-creators-${new Date().toISOString().slice(0,10)}.json`;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        URL.revokeObjectURL(url);
        });

        importBtn.addEventListener('click', () => fileInput.click());

        fileInput.addEventListener('change', (e) => {
        const file = e.target.files[0];
        if (!file) return;
        
        const reader = new FileReader();
        reader.onload = (event) => {
            try {
                const newData = JSON.parse(event.target.result);
                if (Array.isArray(newData) && newData.length > 0) {
                if (confirm(`确定导入 ${newData.length} 条画师数据吗？这将覆盖当前数据。`)) {
                    creators = newData;
                    localStorage.setItem('kemonoCreators', JSON.stringify(creators));
                    renderCreators();
                }
                } else {
                alert('无效的数据格式！');
                }
            } catch (err) {
                alert('解析JSON文件失败！');
            }
        };
        reader.readAsText(file);
        fileInput.value = '';
        });

        clearBtn.addEventListener('click', () => {
        if (confirm('确定要清空所有画师数据吗？此操作不可撤销！')) {
            creators = [];
            localStorage.setItem('kemonoCreators', JSON.stringify(creators));
            renderCreators();
        }
        });

        renderCreators();
    </script>
</body>
</html>